//UNCONVERTED...UNTESTED

using System.Collections.Generic;
using System;
using UnityEngine;
//import java.util.List;
//import java.util.Random;
//
//import javax.swing.*;
//import java.awt.*;
public class Game : MonoBehaviour
{
	public Player playerOne, playerTwo;
	public Card nullCard;
	public UIHandler dp;
	
	public void awake()
	{
		CardReader reader = new CardReader();
		nullCard= new Card("no card.","Effects were negated.","Nothing",0,0,"Nothing");
		playerOne = new Player();
		playerTwo = new Player();
		playerOne.setDeck(reader.parseDeck(reader.list1, playerOne, playerTwo));
		playerTwo.setDeck(reader.parseDeck(reader.list2, playerTwo, playerOne));
		playerOne.drawHand();
		playerTwo.drawHand();
        // Create a Date object for time now.
	}
	
	public Player getPlayerOne()
	{
		return playerOne;
	}
	
	public Player getPlayerTwo()
	{
		return playerTwo;
	}
	
	public void addDisplayPanel(UIHandler display)
	{
		dp = display;
	}
	
/* 	public static void main(String[] args)
	{
		Game g = new Game();
		JFrame frame = new JFrame("Ellis Island: Social Conflict Prototype");
		DisplayPanel display = new DisplayPanel(g);
		g.addDisplayPanel(display);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setPreferredSize(new Dimension(800,504));
		frame.add(display);
		frame.pack();
		frame.setVisible(true);
		display.blackButtons();
		// JOptionPane.showMessageDialog(display,"Player One's Turn");
		display.updateButtons();
		
	} */
	
	
//need to handle null card checks.
	public void runTurn(Card player1Card, Card player2Card){
		int cardSpeed1= player1Card.getPriority();
		int cardSpeed2= player2Card.getPriority();
		if (cardSpeed1>cardSpeed2){
			runP1thenP2(player1Card,player2Card);
		}
		else if(cardSpeed1<cardSpeed2){
			runP2thenP1(player1Card,player2Card);
		}
		else{
			int speedP1=playerOne.getSpeed();
			int speedP2=playerTwo.getSpeed();
			if(speedP1>speedP2){
				runP1thenP2(player1Card,player2Card);
			}
			else if(speedP1<speedP2){
				runP2thenP1(player1Card,player2Card);
			}
			else{
				System.Random rand= new System.Random();
				int check= rand.Next()%2;
				if (check==0){
					runP1thenP2(player1Card,player2Card);
				}
				else{
					runP2thenP1(player1Card,player2Card);
				}
				
			}
		}
		playerOne.useEgo((int)(player1Card.getCost() * playerOne.egoCostRate));
		playerTwo.useEgo((int)(player2Card.getCost() * playerTwo.egoCostRate));
		
		incrementChargeWheel(playerOne, player1Card);
		incrementChargeWheel(playerTwo, player2Card);
		
		playerOne.getTower().turnTowerCheck();
		playerTwo.getTower().turnTowerCheck();
		
		
//		if (winLoseCheck() > 0)
//		{
//			if (// JOptionPane.showConfirmDialog(dp, "Would you like to restart the conflict?","", // JOptionPane.YES_NO_OPTION) == // JOptionPane.YES_OPTION)
//			{
//				playerOne.reset();
//				playerTwo.reset();
//			}
//			else
//			{
//				System.exit(0);
//			}
//		}
		
		//draw cards
		if (playerOne.getHand().Count < 8) playerOne.drawCard();
		if (playerTwo.getHand().Count < 8) playerTwo.drawCard();
		playerOne.regenEgo();
		playerTwo.regenEgo();
	}
	
	public int winLoseCheck(){
		int playerOneHeight=playerOne.getTower().getHeight();
		int playerTwoHeight=playerTwo.getTower().getHeight();
		if (playerOneHeight>=100 || playerTwoHeight<=0){
		//	// JOptionPane.showMessageDialog(dp, "Player one wins!");
			return 1;
		}
		else if(playerTwoHeight>=100 || playerOneHeight<=0){
		//	// JOptionPane.showMessageDialog(dp, "Player two wins!");
			return 2;
		}
		else{
			return 0;
		}
	}
	
	public void incrementChargeWheel(Player p, Card c){
		String cardType= c.getType();
		if(cardType.Equals("Logic")){
			p.getCharges().incrementCharge(ChargeWheel.chargeType.LOGIC);
		}
		else if(cardType.Equals("Ethics")){
			p.getCharges().incrementCharge(ChargeWheel.chargeType.ETHICS);
		}
		else if(cardType.Equals("Passion")){
			p.getCharges().incrementCharge(ChargeWheel.chargeType.PASSION);
		}
		else if(cardType.Equals("Deception")){
			p.getCharges().incrementCharge(ChargeWheel.chargeType.DECEPTION);
		}
	}
	
	
	public void runP1thenP2(Card player1Card, Card player2Card){
		if(player1Card !=nullCard){
			for(int i=0; i< playerOne.getBuffs().Count; i++) playerOne.getBuffs()[i].execute(player1Card);
			List<EffectActive> player1Effects = player1Card.getActives();
			// JOptionPane.showMessageDialog(dp, "Player One plays " + player1Card.getName() +". " + player1Card.getActiveDescription());
			for(int i=0;i<player1Effects.Count;i++){
				EffectActive currentEffect= player1Effects[i];
				currentEffect.execute();
			}
			
		}
		if(player2Card != nullCard){
			for(int i=0; i< playerTwo.getBuffs().Count; i++) playerTwo.getBuffs()[i].execute(player2Card);
			List<EffectActive> player2Effects = player2Card.getActives();
			// JOptionPane.showMessageDialog(dp, "Player Two plays " + player2Card.getName() +". " + player2Card.getActiveDescription());
			for(int i=0;i<player2Effects.Count;i++){
				EffectActive currentEffect= player2Effects[i];
				currentEffect.execute();
			}
			
		}
	}
	
	public void runP2thenP1(Card player1Card, Card player2Card){
		if(player2Card != nullCard){
			for(int i=0; i< playerTwo.getBuffs().Count; i++) playerTwo.getBuffs()[i].execute(player2Card);
			List<EffectActive> player2Effects = player2Card.getActives();
			// JOptionPane.showMessageDialog(dp, "Player Two plays " + player2Card.getName() +". " + player2Card.getActiveDescription());
			for(int i=0;i<player2Effects.Count;i++){
				EffectActive currentEffect= player2Effects[i];
				currentEffect.execute();
			}
			
		}
		if(player1Card !=nullCard){
			for(int i=0; i< playerOne.getBuffs().Count; i++) playerOne.getBuffs()[i].execute(player1Card);
			List<EffectActive> player1Effects = player1Card.getActives();
			// JOptionPane.showMessageDialog(dp, "Player One plays " + player1Card.getName() +". " + player1Card.getActiveDescription());
			for(int i=0;i<player1Effects.Count;i++){
				EffectActive currentEffect= player1Effects[i];
				currentEffect.execute();
			}
			
		}
		
		
	}
	
	
	
	public void resetGame()
	{
		playerOne.reset();
		playerTwo.reset();
		
	
	}

	public void updateBuffs() {
		for(int i=0; i<playerOne.getBuffs().Count; i++){
			playerOne.getBuffs()[i].duration --;
			Debug.Log(playerOne.getBuffs()[i].duration);
			if(playerOne.getBuffs()[i].duration == 0){
				playerOne.getBuffs()[i].cleanse();
				playerOne.getBuffs().RemoveAt(i);
			}
		}
		for(int i=0; i<playerTwo.getBuffs().Count; i++){
			playerTwo.getBuffs()[i].duration --;
			Debug.Log(playerTwo.getBuffs()[i].duration);
			if(playerTwo.getBuffs()[i].duration == 0){
				playerTwo.getBuffs()[i].cleanse();
				playerTwo.getBuffs().RemoveAt(i);
			}
		}
	}
	
}